﻿using UnityEngine;
using System.Collections;

public enum PrefabTypes
{
    CommonMsgBox,
    LoadingWindow,
	MapButton,
	Card,
    Card1,
	TeamCard,
	Panel_SkillBar1,
	Panel_SkillBar2,
	Panel_SkillBar3,
	Panel_Shop_ItemBtn,
	UI_FriendBar_1,
	UI_FriendBar_2,
	Panel_notice,
	FriendBar,
	FriendBar_1,
	Panel_MiniMap,
    map_demo01_Mini,
    map_demo03_Mini,
	map_M1_1_Mini,
	map_M1_2_Mini,
	map_M1_3_Mini,
	map_M1_4_Mini,
    map_M1_5_Mini,
    map_pvp1_Mini,
    map_pvp2_Mini,
    map_pvp3_Mini,
    map_pve1_1_Mini,
    map_pve1_2_Mini,
    map_pve1_3_Mini,
    map_pve1_4_Mini,
    map_pve1_5_Mini,
    map_pve1_6_Mini,
    map_pve1_7_Mini,
    map_tutorial_Mini,
	Panel_ContinuePopUp_1,
	Panel_ContinuePopUp_2,
	Panel_RandomBox,
	MonsterRewardCard,
	MaterialUnitCard_Temp,
	Panel_Emoticon,
	RewardCash,
	RewardCoin,
	RewardItem,
	RewardUnit,
	Panel_Button_mail,
	RankBar,
	Panel_ChooseGameOrder,
	ChanceSkill,
	Panel_MessagePopup,
	UI_Shop_ItemBtn,
    Fx_VictoryIngame01,
	Panel_GameRewardResult,
	RewardItemCell,
	RewardPointCell
}

public class LTPrefabManager : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    static public GameObject InstantiatePrefab(PrefabTypes _prefabTypes, Vector3 _position, Quaternion _rotation)
    {
        GameObject _tmpObj = Resources.Load("Prefabs/" + _prefabTypes.ToString()) as GameObject;

        if (_tmpObj == null)
        {
			_tmpObj = Resources.Load("Prefabs/UI/" + _prefabTypes.ToString()) as GameObject;
			if (_tmpObj == null)
        	{
				_tmpObj = Resources.Load("Prefabs/UI_New/" + _prefabTypes.ToString()) as GameObject;
				if (_tmpObj == null)
        		{
					_tmpObj = Resources.Load("Prefabs/FX/" + _prefabTypes.ToString()) as GameObject;
					if (_tmpObj == null)
					{
		            	Debug.Log("ERROR - LodePrefabs : " + _prefabTypes);
		            	return null;
					}
				}
			}
			_tmpObj = Instantiate(_tmpObj, _position, _rotation) as GameObject;
            Resources.UnloadUnusedAssets();
            return _tmpObj;
        }
        else
        {
            _tmpObj = Instantiate(_tmpObj, _position, _rotation) as GameObject;
            Resources.UnloadUnusedAssets();
            return _tmpObj;
        }
    }
	
	public static GameObject InstantiateMap(string mapName, bool isBack, string postFix)
	{
		if(isBack)
		{
			GameObject tmePrefab = Resources.Load("Prefabs/Map_back/" + mapName+postFix) as GameObject;
			if(tmePrefab!=null)
			{
	        	GameObject _tmpObj = Instantiate(tmePrefab) as GameObject;
				if(_tmpObj==null)
					Debug.LogError("mapname not exist "+mapName);
				return _tmpObj;
			}
			else
			{
				if(postFix == "_back")
				{
					tmePrefab = Resources.Load("Prefabs/Map_back/map_test_back") as GameObject;
					GameObject _tmpObj = Instantiate(tmePrefab) as GameObject;
					return _tmpObj;
				}
			}
		}
		else
		{
        	GameObject tmePrefab = Resources.Load("Prefabs/Map_tile/" + mapName) as GameObject;
			if(tmePrefab!=null)
			{
	        	GameObject _tmpObj = Instantiate(tmePrefab) as GameObject;
				if(_tmpObj==null)
					Debug.LogError("mapname not exist "+mapName);
				return _tmpObj;
			}
		}
		
		Resources.UnloadUnusedAssets();
		return null;
	}
	
    public static MonoBehaviour InstantiateC(PrefabTypes _prefabTypes)
    {
        return InstantiateC(_prefabTypes, Vector3.zero, Quaternion.identity);
    }

    public static MonoBehaviour InstantiateC(PrefabTypes _prefabTypes, Vector3 _position, Quaternion _rotation)
    {
        GameObject obj = InstantiatePrefab(_prefabTypes, _position, _rotation);
        MonoBehaviour mono = obj.GetComponent(typeof(MonoBehaviour)) as MonoBehaviour;

        if (mono == null)
            Debug.Log("ERROR - InstantiateC : It doesn's have Monobehavior");
		
//		Debug.Log(mono.gameObject.name);

        return mono;
    }

    public static MonoBehaviour InstantiateMiniMap(PrefabTypes _prefabTypes)
    {
        GameObject obj = Resources.Load("Prefabs/" + _prefabTypes.ToString()) as GameObject;
        if (obj != null)
        {
            obj = Instantiate(obj) as GameObject;
            Resources.UnloadUnusedAssets();
        }
        MonoBehaviour mono = obj.GetComponent(typeof(MonoBehaviour)) as MonoBehaviour;

        if (mono == null)
            Debug.Log("ERROR - InstantiateC : It doesn's have Monobehavior");

        //		Debug.Log(mono.gameObject.name);

        return mono;
    }

	public static Vector3 GetPrefabOriginalPosition(PrefabTypes _prefabTypes)
	{
		GameObject _tmpObj = Resources.Load("Prefabs/" + _prefabTypes.ToString()) as GameObject;

		if (_tmpObj == null)
		{
			_tmpObj = Resources.Load("Prefabs/UI/" + _prefabTypes.ToString()) as GameObject;
			if (_tmpObj == null)
			{
				_tmpObj = Resources.Load("Prefabs/UI_New/" + _prefabTypes.ToString()) as GameObject;
				if (_tmpObj == null)
				{
					_tmpObj = Resources.Load("Prefabs/FX/" + _prefabTypes.ToString()) as GameObject;
					if (_tmpObj == null)
					{
						Debug.Log("ERROR - LodePrefabs : " + _prefabTypes);
						return Vector3.zero;
					}
				}
			}
		}
		return _tmpObj.transform.localPosition;
	}

    public static Vector3 GetPrefabOriginalScale(PrefabTypes _prefabTypes)
    {
        GameObject _tmpObj = Resources.Load("Prefabs/" + _prefabTypes.ToString()) as GameObject;

        if (_tmpObj == null)
        {
            _tmpObj = Resources.Load("Prefabs/UI/" + _prefabTypes.ToString()) as GameObject;
            if (_tmpObj == null)
            {
                _tmpObj = Resources.Load("Prefabs/UI_New/" + _prefabTypes.ToString()) as GameObject;
                if (_tmpObj == null)
                {
                    _tmpObj = Resources.Load("Prefabs/FX/" + _prefabTypes.ToString()) as GameObject;
                    if (_tmpObj == null)
                    {
                        Debug.Log("ERROR - LodePrefabs : " + _prefabTypes);
                        return Vector3.zero;
                    }
                }
            }
        }
        return _tmpObj.transform.localScale;
    }
}
